home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsparams.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.6 KB  |  414 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsparams.c,v 1.2 2000/09/19 19:00:30 lpd Exp $ */
  20. /* Generic parameter list serializer & expander */
  21.  
  22. /* Initial version 2/1/98 by John Desrosiers (soho@crl.com) */
  23. /* 11/16/98 L. Peter Deutsch (ghost@aladdin.com) edited to remove names
  24.    put_bytes, put_word which conflicted with other modules */
  25.  
  26. #include "gx.h"
  27. #include "memory_.h"
  28. #include "gserrors.h"
  29. #include "gsparams.h"
  30.  
  31. /* ----------- Local Type Decl's ------------ */
  32. typedef struct {
  33.     byte *buf;            /* current buffer ptr */
  34.     byte *buf_end;        /* end of buffer */
  35.     unsigned total_sizeof;    /* current # bytes in buf */
  36. } WriteBuffer;
  37.  
  38. /* ---------- Forward refs ----------- */
  39. private void
  40. ptr_align_to(P2(
  41.         const byte ** src,    /* pointer to align */
  42.         unsigned alignment    /* alignment, must be power of 2 */
  43.         ));
  44. private void
  45. wb_put_word(P2(
  46.         unsigned source,    /* number to put to buffer */
  47.         WriteBuffer * dest    /* destination descriptor */
  48.         ));
  49. private void
  50. wb_put_bytes(P3(
  51.          const byte * source,    /* bytes to put to buffer */
  52.          unsigned source_sizeof,    /* # bytes to put */
  53.          WriteBuffer * dest        /* destination descriptor */
  54.          ));
  55. private void
  56. wb_put_alignment(P2(
  57.          unsigned alignment,    /* alignment to match, must be power 2 */
  58.          WriteBuffer * dest    /* destination descriptor */
  59.          ));
  60.  
  61. /* Get word compressed with wb_put_word */
  62. private unsigned        /* decompressed word */
  63. buf_get_word(P1(
  64.         const byte ** src    /* UPDATES: ptr to src buf ptr */
  65.         ));
  66.  
  67.  
  68. /* ------------ Serializer ------------ */
  69. /* Serialize the contents of a gs_param_list (including sub-dicts) */
  70. int                /* ret -ve err, else # bytes needed to represent param list, whether */
  71.  
  72. /* or not it actually fit into buffer. List was successully */
  73.  
  74. /* serialized only if if this # is <= supplied buf size. */
  75. gs_param_list_serialize(
  76.                gs_param_list * list,    /* root of list to serialize */
  77.                     /* list MUST BE IN READ MODE */
  78.                byte * buf,    /* destination buffer (can be 0) */
  79.                int buf_sizeof    /* # bytes available in buf (can be 0) */
  80. )
  81. {
  82.     int code = 0;
  83.     int temp_code;
  84.     gs_param_enumerator_t key_enum;
  85.     gs_param_key_t key;
  86.     WriteBuffer write_buf;
  87.  
  88.     write_buf.buf = buf;
  89.     write_buf.buf_end = buf + (buf ? buf_sizeof : 0);
  90.     write_buf.total_sizeof = 0;
  91.     param_init_enumerator(&key_enum);
  92.  
  93.     /* Each item is serialized as ("word" means compressed word):
  94.      *  word: key sizeof + 1, or 0 if end of list/dict
  95.      *  word: data type(gs_param_type_xxx)
  96.      *  byte[]: key, including trailing \0 
  97.      *  (if simple type)
  98.      *   byte[]: unpacked representation of data
  99.      *  (if simple array or string)
  100.      *   byte[]: unpacked mem image of gs_param_xxx_array structure
  101.      *   pad: to array alignment
  102.      *   byte[]: data associated with array contents
  103.      *  (if string/name array)
  104.      *   byte[]: unpacked mem image of gs_param_string_array structure
  105.      *   pad: to void *
  106.      *   { gs_param_string structure mem image;
  107.      *     data associated with string;
  108.      *   } for each string in array
  109.      *  (if dict/dict_int_keys)
  110.      *   word: # of entries in dict,
  111.      *   pad: to void *
  112.      *   dict entries follow immediately until end-of-dict
  113.      *
  114.      * NB that this format is designed to allow using an input buffer
  115.      * as the direct source of data when expanding a gs_c_param_list
  116.      */
  117.     /* Enumerate all the keys; use keys to get their typed values */
  118.     while ((code = param_get_next_key(list, &key_enum, &key)) == 0) {
  119.     int value_top_sizeof;
  120.     int value_base_sizeof;
  121.  
  122.     /* Get next datum & put its type & key to buffer */
  123.     gs_param_typed_value value;
  124.     char string_key[256];
  125.  
  126.     if (sizeof(string_key) < key.size + 1) {
  127.         code = gs_note_error(gs_error_rangecheck);
  128.         break;
  129.     }
  130.     memcpy(string_key, key.data, key.size);
  131.     string_key[key.size] = 0;
  132.     if ((code = param_read_typed(list, string_key, &value)) != 0) {
  133.         code = code > 0 ? gs_note_error(gs_error_unknownerror) : code;
  134.         break;
  135.     }
  136.     wb_put_word((unsigned)key.size + 1, &write_buf);
  137.     wb_put_word((unsigned)value.type, &write_buf);
  138.     wb_put_bytes((byte *) string_key, key.size + 1, &write_buf);
  139.  
  140.     /* Put value & its size to buffer */
  141.     value_top_sizeof = gs_param_type_sizes[value.type];
  142.     value_base_sizeof = gs_param_type_base_sizes[value.type];
  143.     switch (value.type) {
  144.         case gs_param_type_null:
  145.         case gs_param_type_bool:
  146.         case gs_param_type_int:
  147.         case gs_param_type_long:
  148.         case gs_param_type_float:
  149.         wb_put_bytes((byte *) & value.value, value_top_sizeof, &write_buf);
  150.         break;
  151.  
  152.         case gs_param_type_string:
  153.         case gs_param_type_name:
  154.         case gs_param_type_int_array:
  155.         case gs_param_type_float_array:
  156.         wb_put_bytes((byte *) & value.value, value_top_sizeof, &write_buf);
  157.         wb_put_alignment(value_base_sizeof, &write_buf);
  158.         value_base_sizeof *= value.value.s.size;
  159.         wb_put_bytes(value.value.s.data, value_base_sizeof, &write_buf);
  160.         break;
  161.  
  162.         case gs_param_type_string_array:
  163.         case gs_param_type_name_array:
  164.         value_base_sizeof *= value.value.sa.size;
  165.         wb_put_bytes((const byte *)&value.value, value_top_sizeof, &write_buf);
  166.         wb_put_alignment(sizeof(void *), &write_buf);
  167.  
  168.         wb_put_bytes((const byte *)value.value.sa.data, value_base_sizeof,
  169.               &write_buf);
  170.         {
  171.             int str_count;
  172.             const gs_param_string *sa;
  173.  
  174.             for (str_count = value.value.sa.size,
  175.              sa = value.value.sa.data; str_count-- > 0; ++sa)
  176.             wb_put_bytes(sa->data, sa->size, &write_buf);
  177.         }
  178.         break;
  179.  
  180.         case gs_param_type_dict:
  181.         case gs_param_type_dict_int_keys:
  182.         wb_put_word(value.value.d.size, &write_buf);
  183.         wb_put_alignment(sizeof(void *), &write_buf);
  184.  
  185.         {
  186.             int bytes_written =
  187.             gs_param_list_serialize(value.value.d.list,
  188.                         write_buf.buf,
  189.              write_buf.buf ? write_buf.buf_end - write_buf.buf : 0);
  190.  
  191.             temp_code = param_end_read_dict(list,
  192.                             (const char *)key.data,
  193.                             &value.value.d);
  194.             if (bytes_written < 0)
  195.             code = bytes_written;
  196.             else {
  197.             code = temp_code;
  198.             if (bytes_written)
  199.                 wb_put_bytes(write_buf.buf, bytes_written, &write_buf);
  200.             }
  201.         }
  202.         break;
  203.  
  204.         default:
  205.         code = gs_note_error(gs_error_unknownerror);
  206.         break;
  207.     }
  208.     if (code < 0)
  209.         break;
  210.     }
  211.  
  212.     /* Write end marker, which is an (illegal) 0 key length */
  213.     if (code >= 0) {
  214.     wb_put_word(0, &write_buf);
  215.     code = write_buf.total_sizeof;
  216.     }
  217.     return code;
  218. }
  219.  
  220.  
  221. /* ------------ Expander --------------- */
  222. /* Expand a buffer into a gs_param_list (including sub-dicts) */
  223. int                /* ret -ve err, +ve # of chars read from buffer */
  224. gs_param_list_unserialize(
  225.                  gs_param_list * list,    /* root of list to expand to */
  226.                     /* list MUST BE IN WRITE MODE */
  227.                  const byte * buf    /* source buffer */
  228. )
  229. {
  230.     int code = 0;
  231.     const byte *orig_buf = buf;
  232.  
  233.     do {
  234.     gs_param_typed_value typed;
  235.     gs_param_name key;
  236.     unsigned key_sizeof;
  237.     int value_top_sizeof;
  238.     int value_base_sizeof;
  239.     int temp_code;
  240.     gs_param_type type;
  241.  
  242.     /* key length, 0 indicates end of data */
  243.     key_sizeof = buf_get_word(&buf);
  244.     if (key_sizeof == 0)    /* end of data */
  245.         break;
  246.  
  247.     /* data type */
  248.     type = (gs_param_type) buf_get_word(&buf);
  249.  
  250.     /* key */
  251.     key = (gs_param_name) buf;
  252.     buf += key_sizeof;
  253.  
  254.     /* Data values */
  255.     value_top_sizeof = gs_param_type_sizes[type];
  256.     value_base_sizeof = gs_param_type_base_sizes[type];
  257.     typed.type = type;
  258.     if (type != gs_param_type_dict && type != gs_param_type_dict_int_keys) {
  259.         memcpy(&typed.value, buf, value_top_sizeof);
  260.         buf += value_top_sizeof;
  261.     }
  262.     switch (type) {
  263.         case gs_param_type_null:
  264.         case gs_param_type_bool:
  265.         case gs_param_type_int:
  266.         case gs_param_type_long:
  267.         case gs_param_type_float:
  268.         break;
  269.  
  270.         case gs_param_type_string:
  271.         case gs_param_type_name:
  272.         case gs_param_type_int_array:
  273.         case gs_param_type_float_array:
  274.         ptr_align_to(&buf, value_base_sizeof);
  275.         typed.value.s.data = buf;
  276.         typed.value.s.persistent = false;
  277.         buf += typed.value.s.size * value_base_sizeof;
  278.         break;
  279.  
  280.         case gs_param_type_string_array:
  281.         case gs_param_type_name_array:
  282.         ptr_align_to(&buf, sizeof(void *));
  283.  
  284.         typed.value.sa.data = (const gs_param_string *)buf;
  285.         typed.value.sa.persistent = false;
  286.         buf += typed.value.s.size * value_base_sizeof;
  287.         {
  288.             int str_count;
  289.             gs_param_string *sa;
  290.  
  291.             for (str_count = typed.value.sa.size,
  292.              sa = (gs_param_string *) typed.value.sa.data;
  293.              str_count-- > 0; ++sa) {
  294.             sa->data = buf;
  295.             sa->persistent = false;
  296.             buf += sa->size;
  297.             }
  298.         }
  299.         break;
  300.  
  301.         case gs_param_type_dict:
  302.         case gs_param_type_dict_int_keys:
  303.         typed.value.d.size = buf_get_word(&buf);
  304.         code = param_begin_write_dict
  305.             (list, key, &typed.value.d, type == gs_param_type_dict_int_keys);
  306.         if (code < 0)
  307.             break;
  308.         ptr_align_to(&buf, sizeof(void *));
  309.  
  310.         code = gs_param_list_unserialize(typed.value.d.list, buf);
  311.         temp_code = param_end_write_dict(list, key, &typed.value.d);
  312.         if (code >= 0) {
  313.             buf += code;
  314.             code = temp_code;
  315.         }
  316.         break;
  317.  
  318.         default:
  319.         code = gs_note_error(gs_error_unknownerror);
  320.         break;
  321.     }
  322.     if (code < 0)
  323.         break;
  324.     if (typed.type != gs_param_type_dict && typed.type != gs_param_type_dict_int_keys)
  325.         code = param_write_typed(list, key, &typed);
  326.     }
  327.     while (code >= 0);
  328.  
  329.     return code >= 0 ? buf - orig_buf : code;
  330. }
  331.  
  332.  
  333. /* ---------- Utility functions -------- */
  334.  
  335. /* Align a byte pointer on the next Nth byte */
  336. private void
  337. ptr_align_to(
  338.         const byte ** src,    /* pointer to align */
  339.         unsigned alignment    /* alignment, must be power of 2 */
  340. )
  341. {
  342.     *src += -(int)ALIGNMENT_MOD(*src, alignment) & (alignment - 1);
  343. }
  344.  
  345. /* Put compressed word repr to a buffer */
  346. private void
  347. wb_put_word(
  348.         unsigned source,    /* number to put to buffer */
  349.         WriteBuffer * dest    /* destination descriptor */
  350. )
  351. {
  352.     do {
  353.     byte chunk = source & 0x7f;
  354.  
  355.     if (source >= 0x80)
  356.         chunk |= 0x80;
  357.     source >>= 7;
  358.     ++dest->total_sizeof;
  359.     if (dest->buf && dest->buf < dest->buf_end)
  360.         *dest->buf++ = chunk;
  361.     }
  362.     while (source != 0);
  363. }
  364.  
  365. /* Put array of bytes to buffer */
  366. private void
  367. wb_put_bytes(
  368.          const byte * source,    /* bytes to put to buffer */
  369.          unsigned source_sizeof,    /* # bytes to put */
  370.          WriteBuffer * dest    /* destination descriptor */
  371. )
  372. {
  373.     dest->total_sizeof += source_sizeof;
  374.     if (dest->buf && dest->buf + source_sizeof <= dest->buf_end) {
  375.     if (dest->buf != source)
  376.         memcpy(dest->buf, source, source_sizeof);
  377.     dest->buf += source_sizeof;
  378.     }
  379. }
  380.  
  381. /* Pad destination out to req'd alignment w/zeros */
  382. private void
  383. wb_put_alignment(
  384.          unsigned alignment,    /* alignment to match, must be power 2 */
  385.          WriteBuffer * dest    /* destination descriptor */
  386. )
  387. {
  388.     static const byte zero =
  389.     {0};
  390.  
  391.     while ((dest->total_sizeof & (alignment - 1)) != 0)
  392.     wb_put_bytes(&zero, 1, dest);
  393. }
  394.  
  395. /* Get word compressed with wb_put_word */
  396. private unsigned        /* decompressed word */
  397. buf_get_word(
  398.         const byte ** src    /* UPDATES: ptr to src buf ptr */
  399. )
  400. {
  401.     unsigned dest = 0;
  402.     byte chunk;
  403.     unsigned shift = 0;
  404.  
  405.     do {
  406.     chunk = *(*src)++;
  407.     dest |= (chunk & 0x7f) << shift;
  408.     shift += 7;
  409.     }
  410.     while (chunk & 0x80);
  411.  
  412.     return dest;
  413. }
  414.